package gmail_test;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;


public class HomePage extends Page {

	@FindBy(xpath = "//a[contains(@href,'logout') and @target='_top']")
	public WebElement linkLogOut;
	
	@FindBy(xpath = "//a[contains(@href, 'https://') and contains(@href, '#inbox')]")
	public WebElement linkInbox;
	
	@FindBy(xpath = "//a[contains(@href,'https://profiles.google.com')]")
	public WebElement linkProfile;
	
	@FindBy(xpath = "//input[@aria-label='Search' and @type='text']")
	public WebElement lineSearch;
	
	@FindBy(xpath = "//button[@aria-label='Search Gmail' and @name='']")
	public WebElement buttonSearch;
	
	public HomePage(WebDriver driver) {
		super(driver);
		this.driver = driver;
	}

	@Override
	public void Open() {
	
	}
	
	public boolean isInbox()
	{
		if(isElementPresent(linkInbox))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public boolean isLoggedIn()
	{
		return isElementPresent(linkLogOut);
	}
	
	public void logOut()
	{
		linkProfile.click();
		linkLogOut.click();
	}
	
	public boolean isTextPresent(WebElement webElement, String text)
	{
		List<WebElement> list =  driver.findElements(By.tagName("table"));

		for(WebElement item : list)
		{
			if(item.getText().contains(text))
			{
				return true;
			}
		}
		
		return false;		
	}
	
	public boolean isTextPresentInTable(String text)
	{
		String find = "//div[@class = 'y6']/span[contains(., '"+text+"')]";
		boolean result = false;
		WebElement element = null;
		
		try
		{
			element = driver.findElement(By.xpath(find));
		
			if(element.isDisplayed())
			{
				result = true;
			}
		}
		catch (NoSuchElementException e)
		{
			System.out.println("return false.");
		}
		
		return result;
	}

}
